package Screens;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.media.MediaException;
import javax.microedition.media.control.VolumeControl;

import resource.Keyboard;
import resource.SettingScreen;
import resource.Sound;

import Game.CanvasGame;

public class Setting extends Screen {

	private Image[] img;
	private Sprite sprite;
	private VolumeControl vc;
	public Setting(CanvasGame _canvas) {
		super(_canvas);
		// TODO Auto-generated constructor stub
		img = new Image[SettingScreen.IMG_LENGHT];
		SettingScreen.currentVolume = canvas.getVolume()/20;
		load_screen();
	}
	public void load_screen() {
		// TODO Auto-generated method stub
		for (int i = 0; i < SettingScreen.IMG_LENGHT; i++) {
			load(i);
		}

		sprite = new Sprite(img[SettingScreen.CUR_VOLUME], 31, 156);
	}
	
	public void un_load_screen() {
		// TODO Auto-generated method stub
		//img = null;
		if(img == null)    return;
        for (int i = 0; i < SettingScreen.IMG_LENGHT; i++) {
            unLoad(i);
        }
	}
	
	public boolean load(int i) {
		/* TODO: load the images here */
	    try {
	    	img[i] = Image.createImage(SettingScreen.imgSource[i]);
	    } catch (Exception ex) {
	    	System.err.println(ex.toString());
	    	return false;
	    }
	    return true;
	}
	
	public void unLoad(int i) {
        /* TODO: make sure the object get's destroyed */
        img[i] = null;
    }

	public void keyEvent(int keycode, int event) {
		// TODO Auto-generated method stub
		if(event == Keyboard.KEY_EVENT_DOWN){
			switch (keycode) {
			case Keyboard.KEY_LEFT:
				SettingScreen.currentVolume = SettingScreen.currentVolume==0?0:SettingScreen.currentVolume-1;
				canvas.setVolume(SettingScreen.currentVolume*20);
				try {
					if(canvas.getVolume()>0){
						vc = (VolumeControl) canvas.sound[Sound.BEEP].getControl("VolumeControl");
						vc.setLevel(canvas.getVolume());
						canvas.sound[Sound.BEEP].start();
					}
				} catch (MediaException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				break;
			case Keyboard.KEY_RIGHT:
				SettingScreen.currentVolume = SettingScreen.currentVolume==5?5:SettingScreen.currentVolume+1;
				canvas.setVolume(SettingScreen.currentVolume*20);
				try {
					if(canvas.getVolume()>0){
						vc = (VolumeControl) canvas.sound[Sound.BEEP].getControl("VolumeControl");
						vc.setLevel(canvas.getVolume());
						canvas.sound[Sound.BEEP].start();
					}
				} catch (MediaException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				break;
			case Keyboard.KEY_OK:
				un_load_screen();
				canvas.setScreen(new Menu(canvas));
				break;
			default:
				break;
			}
		}
	}

	public void ActionClick(int i) {
		// TODO Auto-generated method stub

	}

	public void update() {
		// TODO Auto-generated method stub

	}
	public void printCurVolume(Graphics g){
		for(int i = 0; i<SettingScreen.currentVolume; i++){
			sprite.setFrame(i);
			sprite.setPosition(30+31*i, 40);
			sprite.paint(g);
		}
	}
	public void paint(Graphics g) {
		// TODO Auto-generated method stub
		g.drawImage(img[SettingScreen.BACKGROUND], 0, 0, Graphics.TOP|Graphics.LEFT);
		g.drawImage(img[SettingScreen.FORM_BG], 10, 30, Graphics.TOP|Graphics.LEFT);
		g.drawImage(img[SettingScreen.VOLUME_BAR], 30, 40, Graphics.TOP|Graphics.LEFT);
		//g.drawImage(img[SettingScreen.CUR_VOLUME], 30, 40, Graphics.TOP|Graphics.LEFT);
		printCurVolume(g);
		g.drawImage(img[SettingScreen.MENUBAR], 0, 240, Graphics.BOTTOM|Graphics.LEFT);
	}

}
